/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sahd.broker.iONetMessageManager;


import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 *
 * @author Romms
 */
public class OutgoingNetMessageManager {
    private Socket socket;

    private OutputStream outputStream;
    private ObjectOutputStream objectOutputStream;
    private Message message;
    private String receiverIpAddress;
    private int outPort;
    private InetAddress inetAddress;

    protected OutgoingNetMessageManager(String serverIp, int outPort){
        this.receiverIpAddress= serverIp;
        this.outPort=outPort;
    }

    private void prepareShipping(Object content, int protocol) throws UnknownHostException, IOException{
        inetAddress= InetAddress.getLocalHost();
        message = new Message(inetAddress.getHostAddress(), content,protocol);
        message.setMacAddress(this.getMacAddress());

        socket = new Socket(receiverIpAddress, outPort);
        outputStream = socket.getOutputStream();
        objectOutputStream = new ObjectOutputStream(outputStream);
    }

    private void prepareShipping(Object content, int protocol, int serviceProtocol) throws UnknownHostException, IOException{
        inetAddress= InetAddress.getLocalHost();
        message = new Message(inetAddress.getHostAddress(), content,protocol,serviceProtocol);
        message.setMacAddress(this.getMacAddress());

        socket = new Socket(receiverIpAddress, outPort);
        outputStream = socket.getOutputStream();
        objectOutputStream = new ObjectOutputStream(outputStream);
    }

    private void prepareShipping(Object content, int protocol, String originalSourceIp, int serviceRequestedProtocol)throws UnknownHostException, IOException {
       inetAddress= InetAddress.getLocalHost();
        message = new Message(inetAddress.getHostAddress(), content,protocol,originalSourceIp,serviceRequestedProtocol );
        message.setMacAddress(this.getMacAddress());

        socket = new Socket(receiverIpAddress, outPort);
        outputStream = socket.getOutputStream();
        objectOutputStream = new ObjectOutputStream(outputStream);
    }

    private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
            'f' };

    public static Appendable append(Appendable a, byte[] bytes) {
        try {
            for (byte b : bytes) {
                a.append(DIGITS[(byte) ((b & 0xF0) >> 4)]);
                a.append(DIGITS[(byte) (b & 0x0F)]);
            }
        }
        catch (IOException ex) {
            // Bla
        }
        return a;
    }

    private String getMacAddress() throws SocketException{

        String out = null;
            try {
                Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
                if (ifs != null) {
                    while (ifs.hasMoreElements()) {
                        NetworkInterface iface = ifs.nextElement();
                        byte[] hardware = iface.getHardwareAddress();
                        if (hardware != null && hardware.length == 6
                                && hardware[1] != (byte) 0xff) {
                            out = append(new StringBuilder(36), hardware).toString();
                            break;
                        }
                    }
                }
            }
            catch (SocketException ex) {
                // Ignore it.
            }
            return out;

    }


    private void finalizeShipping() throws IOException{
        objectOutputStream.flush();
        objectOutputStream.close();
        outputStream.flush();
        outputStream.close();
        socket.close();

    }

    protected void sendMessage(Object content, int protocol) throws UnknownHostException, IOException{
        this.prepareShipping(content, protocol);
        objectOutputStream.writeObject(message);
        this.finalizeShipping();
    }

        protected void sendMessage(Object content, int protocol, int serviceRequestedProtocol) throws UnknownHostException, IOException{
        this.prepareShipping(content, protocol, serviceRequestedProtocol);
        objectOutputStream.writeObject(message);
        this.finalizeShipping();
    }

    void sendMessage(Object content, int protocol, String originalSourceIp, int serviceRequestedProtocol) throws UnknownHostException, IOException {
        this.prepareShipping(content, protocol, originalSourceIp, serviceRequestedProtocol);
        objectOutputStream.writeObject(message);
        this.finalizeShipping();
    }


    protected String getServerIpAddress() {
        return receiverIpAddress;
    }

    protected void setReceiverIpAddress(String serverIpAddress) {
        this.receiverIpAddress = serverIpAddress;
    }
    protected int getOutPort() {
        return outPort;
    }

    protected void setOutPort(int outPort) {
        this.outPort = outPort;
    }




}
